package cn.felord.debezium.service;

import cn.felord.debezium.VO.*;
import cn.felord.debezium.dao.DetectionRlMapper;
import cn.felord.debezium.utils.NumberUtils;
import cn.felord.debezium.utils.String2AcsII16;
import cn.felord.debezium.utils.TcpClientPusDuan;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

import static cn.felord.debezium.utils.ChineseToHex.chineseToHex;

/**
 * 铸造一：192.168.1.41
 * 铸造二：192.168.1.61（485）
 * 7       铸造 1234    zhu2     567
 * 不要用小黄人  寄存器发送的话 只发 10进制 转四位数16进制的数值 不够补00
 * 点位的可以  材质 发的是 16进制的字符串 0 对 32 1对31   一个对一个
 *
 * 字库质检跟其他格式分开，牌号 材质就是 起止位
 */
@Slf4j
@Service
public class PushServiceNew {
    @Value("${zhu1ip}")
    String zhu1ip;
    @Value("${zhu1port}")
    String zhu1port;
    @Value("${zhu2ip}")
    String zhu2ip;
    @Value("${zhu2port}")
    String zhu2port;
    @Autowired
    DetectionRlMapper detectionRlMapper;

    public void pushMeltingMeasurement(TbMeltingMeasurementVO tbMeltingMeasurementVO) {
        log.info("开始推送tb_meltinh_mesurement:");
        String factoryCode = tbMeltingMeasurementVO.getFactoryCode();
        String jz = "00 01 00 00 00 1B 01 10 00 F8 00 01 02";
        String o1 = "00 01 00 00 00 1B 01 10 00 F5 00 01 02";// 5/6同 1/2
        String o2 = "00 01 00 00 00 1B 01 10 00 F6 00 01 02";
        String o7 = "00 01 00 00 00 1B 01 10 00 F7 00 01 02";
        String cwValue = tbMeltingMeasurementVO.getCwValue();
        String cwValue16 = String2AcsII16.int10to16_4(Integer.valueOf(cwValue));//转16 tong
        String rlluNumber = tbMeltingMeasurementVO.getRlluNumber();
        if(rlluNumber.endsWith("3") || rlluNumber.equals("03") ||
                rlluNumber.endsWith("4") || rlluNumber.equals("04")
        ){
            String data = jz + cwValue16;
            if(factoryCode.equals("1") || factoryCode.equals("01")){
                TcpClientPusDuan.push(zhu1ip,zhu1port,data,jz);
            }else {
                TcpClientPusDuan.push(zhu2ip,zhu2port,data,jz);
            }
        }
        if(rlluNumber.endsWith("1") || rlluNumber.equals("01")){
            String data = o1 + cwValue16;
            TcpClientPusDuan.push(zhu1ip,zhu1port,data,jz);
        }else if(rlluNumber.endsWith("2") || rlluNumber.equals("02")){
            String data = o2 + cwValue16;
            TcpClientPusDuan.push(zhu1ip,zhu1port,data,jz);
        }else if(rlluNumber.endsWith("5") || rlluNumber.equals("05")){
            String data = o1 + cwValue16;
            TcpClientPusDuan.push(zhu2ip,zhu2port,data,jz);
        }else if(rlluNumber.endsWith("6") || rlluNumber.equals("06")){
            String data = o2 + cwValue16;
            TcpClientPusDuan.push(zhu2ip,zhu2port,data,jz);
        }else if(rlluNumber.endsWith("7") || rlluNumber.equals("07")){
            String data = o7 + cwValue16;
            TcpClientPusDuan.push(zhu2ip,zhu2port,data,jz);
        }

    }
    public void pushFeeding(TbUploadTruckFeedingVO tbUploadTruckFeedingVO) throws InterruptedException {
        log.info("开始推送tb_upload_truck_feeding:");
        log.info(tbUploadTruckFeedingVO.getFactoryCode() + "车间推送加料数据：" + tbUploadTruckFeedingVO.toString());
        String fg16 = String2AcsII16.int10to16_4(tbUploadTruckFeedingVO.getFg().intValue());
        String hll16 = String2AcsII16.int10to16_4(tbUploadTruckFeedingVO.getHll().intValue());
        String st16 = String2AcsII16.int10to16_4(tbUploadTruckFeedingVO.getSt().intValue());
        String tf16 = String2AcsII16.int10to16_4(tbUploadTruckFeedingVO.getTf().intValue());
        String fgp16 = String2AcsII16.int10to16_4(tbUploadTruckFeedingVO.getFgp().intValue());
        String hllp16 = String2AcsII16.int10to16_4(tbUploadTruckFeedingVO.getHllp().intValue());
        String stp16 = String2AcsII16.int10to16_4(tbUploadTruckFeedingVO.getStp().intValue());
        String tfp16 = String2AcsII16.int10to16_4(tbUploadTruckFeedingVO.getTfp().intValue());
        String c16 = String2AcsII16.int10to16_4(tbUploadTruckFeedingVO.getC().intValue());
        String si16 = String2AcsII16.int10to16_4(tbUploadTruckFeedingVO.getSi().intValue());
        String mn16 = String2AcsII16.int10to16_4(tbUploadTruckFeedingVO.getMn().intValue());
        String s16 = String2AcsII16.int10to16_4(tbUploadTruckFeedingVO.getS().intValue());
        String p16 = String2AcsII16.int10to16_4(tbUploadTruckFeedingVO.getP().intValue());
        String cr16 = String2AcsII16.int10to16_4(tbUploadTruckFeedingVO.getCr().intValue());
        String cu16 = String2AcsII16.int10to16_4(tbUploadTruckFeedingVO.getCu().intValue());
        /**
         *  1 推送实时加料牌号【配比】名称 一、 二、 三、 四、五、六 实际数据地址
         */
        log.info("推送加料选择的牌号配比名称：");
        String zk4 = "00 01 00 00 00 1B 01 10 00 15 00 01 02";//配方feedingVO
        String zk5 = "00 01 00 00 00 1B 01 10 00 17 00 01 02";
        String zk6 = "00 01 00 00 00 1B 01 10 00 19 00 01 02";
        String pf = tbUploadTruckFeedingVO.getFormula().substring(0,1);
        log.info(pf);
        String pf16 = chineseToHex(pf);//字符串直接转 16进制，一个字符对应两位的16进制
        String furnaceNum = tbUploadTruckFeedingVO.getFurnaceNum();
        if (furnaceNum.endsWith("1") || furnaceNum.equals("01")) {
            String data = zk4 + pf16;
            TcpClientPusDuan.push(zhu1ip,zhu1port,data,false);
        }else if(furnaceNum.endsWith("2") || furnaceNum.equals("02")){
            String data = zk5 + pf16;
            TcpClientPusDuan.push(zhu1ip,zhu1port,data,false);
        }else if (furnaceNum.endsWith("5") || furnaceNum.equals("05")) {
            String data = zk4 + pf16;
            TcpClientPusDuan.push(zhu2ip,zhu2port,data,false);
        }else if(furnaceNum.endsWith("6") || furnaceNum.equals("06")){
            String data = zk5 + pf16;
            TcpClientPusDuan.push(zhu2ip,zhu2port,data,false);
        }else if(furnaceNum.endsWith("7") || furnaceNum.equals("07")){
            String data = zk6 + pf16;
            TcpClientPusDuan.push(zhu2ip,zhu2port,data,false);
        }
        if(tbUploadTruckFeedingVO.getFormula() == null){
            log.info("配方关联为空：" + tbUploadTruckFeedingVO.getFormula());
            return;
        }
        log.info("信息完整推送理论配方信息 增碳剂：");
        Thread.sleep(200);//休息200毫秒
        /**
         * 2 推送 配比参数值 30% 20%。。。。。
         */
        log.info("2 推送配比参数值");
        String zu1 = "00 01 00 00 00 1B 01 10 00 C8 00 0B 16";//00 01 00 00 00 1B 01 10 00 0A 00 04 08
        String zu2 = "00 01 00 00 00 1B 01 10 00 D3 00 0B 16";//00 01 00 00 00 1B 01 10 00 0A 00 04 08
        String zu3 = "00 01 00 00 00 1B 01 10 00 DE 00 0B 16";//00 01 00 00 00 1B 01 10 00 0A 00 04 08
        String zData = fgp16 + hllp16 + stp16 + tfp16 + c16 + si16 + mn16 +  s16 + p16 +  cr16 + cu16;
        if (furnaceNum.endsWith("1") || furnaceNum.equals("01")) {
            String data = zu1 + zData;
            TcpClientPusDuan.push(zhu1ip,zhu1port,data,zu1.replaceAll(" ","").length());
        }else if(furnaceNum.endsWith("2") || furnaceNum.equals("02")){
            String data = zu2 + zData;
            TcpClientPusDuan.push(zhu1ip,zhu1port,data,zu2.replaceAll(" ","").length());
        }else if (furnaceNum.endsWith("5") || furnaceNum.equals("05")) {
            String data = zu1 + zData;
            TcpClientPusDuan.push(zhu2ip,zhu2port,data,zu1.replaceAll(" ","").length());
        }else if(furnaceNum.endsWith("6") || furnaceNum.equals("06")){
            String data = zu2 + zData;
            TcpClientPusDuan.push(zhu2ip,zhu2port,data,zu2.replaceAll(" ","").length());
        }else if(furnaceNum.endsWith("7") || furnaceNum.equals("07")){
            String data = zu3 + zData;
            TcpClientPusDuan.push(zhu2ip,zhu2port,data,zu3.replaceAll(" ","").length());
        }
        /**
         *  3 推送实时加料重量
         */
        log.info("3 推送实时加料重量");
        Thread.sleep(200);//休息200毫秒
        String zu1real = "00 01 00 00 00 1B 01 10 00 E9 00 04 08";
        String zu2real = "00 01 00 00 00 1B 01 10 00 ED 00 04 08";
        String zu3real = "00 01 00 00 00 1B 01 10 00 F1 00 04 08";
        String zurealData = fg16 + hll16 + st16 + tf16;
        if (furnaceNum.endsWith("1") || furnaceNum.equals("01")) {
            String data = zu1real + zurealData;
            TcpClientPusDuan.push(zhu1ip,zhu1port,data,zu1real);
        }else if(furnaceNum.endsWith("2") || furnaceNum.equals("02")){
            String data = zu2real + zurealData;
            TcpClientPusDuan.push(zhu1ip,zhu1port,data,zu1real);
        }else if (furnaceNum.endsWith("5") || furnaceNum.equals("05")) {
            String data = zu1real + zurealData;
            TcpClientPusDuan.push(zhu2ip,zhu2port,data,zu1real);
        }else if(furnaceNum.endsWith("6") || furnaceNum.equals("06")){
            String data = zu2real + zurealData;
            TcpClientPusDuan.push(zhu2ip,zhu2port,data,zu1real);
        }else if(furnaceNum.endsWith("7") || furnaceNum.equals("07")){
            String data = zu3real + zurealData;
            TcpClientPusDuan.push(zhu2ip,zhu2port,data,zu1real);
        }
    }
    public void pushDetectRl(DetectRlVO detectRlVO) throws InterruptedException {
        log.info("开始推送tb_detection_rl:");
        String zjlx = detectRlVO.getZjlx();//原铁水
        log.info(detectRlVO.toString());
        log.info(detectRlVO.getPlant() + "车间推送材质和炉次号：");
        /**
         * 1 推送材质字库
         */
        String furnaceNum = detectRlVO.getFurnaceNum();
        String zkcz1 = "00010000001B0110000100050a";
        String zkcz2 = "00010000001B0110000600050a";
        String zkcz3 = "00010000001B0110000B00050a";//
//        炉次号   字库区域7：数据地址31 数据长度11 去掉25编程 9
//        字库区域8：数据地址41 数据长度12
//        字库区域9：数据地址51 数据长度12
        String zklch1 = "00 01 00 00 00 1B 00 10 00 1F 00 09 12";//31 去掉25
        String zklch2 = "00 01 00 00 00 1B 00 10 00 29 00 09 12";//41
        String zklch3 = "00 01 00 00 00 1B 00 10 00 33 00 09 12";//241225_103A  51
        String cz = detectRlVO.getLable1() + "  ";
        if(detectRlVO.getLable1().length() > "HCCM".length()){
            cz = detectRlVO.getLable1();
            if(cz.length() == "G3000".length()){
                cz = cz +" ";
            }
        }
        String cz16 = chineseToHex(cz);//字符串直接转 16进制，一个字符对应两位的16进制 //几个16进制长度
        String lch16 = chineseToHex(detectRlVO.getMeltNum().substring(2));//字符串直接转 16进制，一个字符对应两位的16进制
        if(furnaceNum.endsWith("1") || furnaceNum.equals("01")){
            TcpClientPusDuan.push(zhu1ip,zhu1port,zkcz1  + cz16 ,false);//材质
            TcpClientPusDuan.push(zhu1ip,zhu1port,zklch1 + lch16,false);//炉次号
        }else if(furnaceNum.endsWith("2") || furnaceNum.equals("02")){
            TcpClientPusDuan.push(zhu2ip,zhu2port,zkcz2  + cz16,false);//材质
            TcpClientPusDuan.push(zhu1ip,zhu1port,zklch2 + lch16,false);//炉次号
        }else if(furnaceNum.endsWith("5") || furnaceNum.equals("05")){
            TcpClientPusDuan.push(zhu1ip,zhu1port, zkcz1  + cz16,false);//材质
            TcpClientPusDuan.push(zhu2ip,zhu2port,zklch1 + lch16,false);//炉次号
        }else if(furnaceNum.endsWith("6") || furnaceNum.equals("06")){
            TcpClientPusDuan.push(zhu2ip,zhu2port, zkcz2  + cz16,false);//材质
            TcpClientPusDuan.push(zhu2ip,zhu2port,zklch2 + lch16,false);//炉次号
        }else if(furnaceNum.endsWith("7") || furnaceNum.equals("07")){
            TcpClientPusDuan.push(zhu2ip,zhu2port, zkcz3  + cz16,false);//材质
            TcpClientPusDuan.push(zhu2ip,zhu2port,zklch3 + lch16,false);//炉次号
        }else {
            log.error( furnaceNum + "炉号识别异常！");
        }
        if(null == furnaceNum){
            log.error("炉号为空！");
            return;
        }
        log.info("推送质检data：");
        Thread.sleep(200);//休息200毫秒
        if(detectRlVO.getSdId() == null){
            log.info("质检查询异常：原铁水成分未通过牌号关联上！");
            return;
        }else if(detectRlVO.getSdzId() == null){
            log.info("质检查询异常：出铁水成分未通过牌号关联上！");
            return;
        }
        log.info("推送质检标准：");
        Integer plant2 = detectRlVO.getPlant();
        if(null == plant2){
            log.error("车间识别异常，车间代码:" + detectRlVO.getPlant());
            return;
        }
        /**
         * 计算补加量和颜色计算 0007 7位数  14 = 0E
         */
        String zk10 = "00 01 00 00 00 1B 01 10 00 3C 00 04 07";
        String zk11 = "00 01 00 00 00 1B 01 10 00 41 00 04 07";
        String zk12 = "00 01 00 00 00 1B 01 10 00 46 00 04 07";
        String zk13 = "00 01 00 00 00 1B 01 10 00 4B 00 04 07";
        String zk14 = "00 01 00 00 00 1B 01 10 00 50 00 04 07";
        String zk15 = "00 01 00 00 00 1B 01 10 00 55 00 04 07";
        String zk16 = "00 01 00 00 00 1B 01 10 00 5A 00 04 07";
        //组2
        String zk17 = "00 01 00 00 00 1B 01 10 00 5F 00 04 07";
        String zk18 = "00 01 00 00 00 1B 01 10 00 64 00 04 07";
        String zk19 = "00 01 00 00 00 1B 01 10 00 69 00 04 07";
        String zk20 = "00 01 00 00 00 1B 01 10 00 6E 00 04 07";
        String zk21 = "00 01 00 00 00 1B 01 10 00 73 00 04 07";
        String zk22 = "00 01 00 00 00 1B 01 10 00 78 00 04 07";
        String zk23 = "00 01 00 00 00 1B 01 10 00 7D 00 04 07";

        //组3
        String zk24 = "00 01 00 00 00 1B 01 10 00 82 00 04 07";
        String zk25 = "00 01 00 00 00 1B 01 10 00 87 00 04 07";
        String zk26 = "00 01 00 00 00 1B 01 10 00 8C 00 04 07";
        String zk27 = "00 01 00 00 00 1B 01 10 00 91 00 04 07";
        String zk28 = "00 01 00 00 00 1B 01 10 00 96 00 04 07";
        String zk29 = "00 01 00 00 00 1B 01 10 00 9B 00 04 07";
        String zk30 = "00 01 00 00 00 1B 01 10 00 A0 00 04 07";

        /**
         * 补加量
         * 应补辅料  传整数值，LED屏处理÷100 顺序：增碳剂-硅铁-锰铁-硫铁-磷-铬-铜
         */
        String color = "0002";//绿色0001红色 0003黄色
        List<ElementVO> elementVOS = detectionRlMapper.selectElements();
        HashMap<String, ElementVO> elementVOHashMap = new HashMap<>();
        for (ElementVO elementVO : elementVOS) {
            elementVOHashMap.put(elementVO.getEName(),elementVO);
        }
        double jlzl = detectionRlMapper.getJlzlByMeltNum(detectRlVO.getMeltNum());
        BigDecimal cbjl = BigDecimal.ZERO;
        BigDecimal sibjl = BigDecimal.ZERO;
        BigDecimal mnbjl = BigDecimal.ZERO;
        BigDecimal sbjl = BigDecimal.ZERO;
        BigDecimal pbjl = BigDecimal.ZERO;
        BigDecimal crbjl = BigDecimal.ZERO;
        BigDecimal cubjl = BigDecimal.ZERO;

        ElementVO c = elementVOHashMap.get("C");
        if(zjlx.equals("原铁水")){
            if( detectRlVO.getC() > detectRlVO.getCMax()){
                color = "0001";
            }else if(detectRlVO.getC() < detectRlVO.getCMin()){
                color = "0003";
                if(c != null){
                    cbjl = new BigDecimal(jlzl + "").multiply(
                            new BigDecimal(detectRlVO.getCMin() + "")
                                    .subtract(new BigDecimal(detectRlVO.getC() + "")))
                            .divide(new BigDecimal(c.getEAbsorb()+"").multiply(new BigDecimal(c.getEEffective()+"")),2,BigDecimal.ROUND_HALF_UP)
                          ;
                    log.info("补加增碳剂:" + cbjl.toString() +"原铁水标准min：" + detectRlVO.getCMin()+"实测值：" + detectRlVO.getC() + "加料总重：" + jlzl);
                }
            }else {
                color = "0002";
            }

        }else {
            if( detectRlVO.getC() > detectRlVO.getCMaxZh()){
                color = "0001";
            }else if(detectRlVO.getC() < detectRlVO.getCMinZh()){
                color = "0003";
                if(c != null){
                    cbjl = new BigDecimal(jlzl + "").multiply(
                            new BigDecimal(detectRlVO.getCMinZh() + "")
                                    .subtract(new BigDecimal(detectRlVO.getC() + "")))
                            .divide(new BigDecimal(c.getEAbsorb()+"").multiply(new BigDecimal(c.getEEffective()+"")),2,BigDecimal.ROUND_HALF_UP)
                            ;
                    log.info("补加增碳剂:" + cbjl.toString() +"原铁水标准min：" + detectRlVO.getCMinZh()+"实测值：" + detectRlVO.getC() + "加料总重：" + jlzl);
                }
            }else {
                color = "0002";
            }
        }
        log.info("C:" + detectRlVO.getC().toString());
        detectRlVO.setC(new BigDecimal(detectRlVO.getC()+"").setScale(3,BigDecimal.ROUND_HALF_UP).doubleValue());
        if(furnaceNum.equals("01") || furnaceNum.equals("1")){
            TcpClientPusDuan.push(zhu1ip,zhu1port,zk10 + color + chineseToHex(detectRlVO.getC().toString()),false);
        }else if(furnaceNum.equals("02") || furnaceNum.equals("2")){
            TcpClientPusDuan.push(zhu1ip,zhu1port,zk17 + color + chineseToHex(detectRlVO.getC().toString()),false);
        }else if(furnaceNum.equals("05") || furnaceNum.equals("5")){
            TcpClientPusDuan.push(zhu2ip,zhu2port,zk10 + color + chineseToHex(detectRlVO.getC().toString()),false);
        }else if(furnaceNum.equals("06") || furnaceNum.equals("6")){
            TcpClientPusDuan.push(zhu2ip,zhu2port,zk17 + color + chineseToHex(detectRlVO.getC().toString()),false);
        }else if(furnaceNum.equals("07") || furnaceNum.equals("7")){
            TcpClientPusDuan.push(zhu2ip,zhu2port,zk24 + color + chineseToHex(detectRlVO.getC().toString()),false);
        }
        ElementVO si = elementVOHashMap.get("Si");
        if(zjlx.equals("原铁水")){
            if( detectRlVO.getSi() > detectRlVO.getSiMax()){
                color = "0001";
            }else if(detectRlVO.getSi() < detectRlVO.getSiMin()){
                color = "0003";
                if(si != null){
                    sibjl = new BigDecimal(jlzl + "").multiply(
                            new BigDecimal(detectRlVO.getSiMin() + "")
                                    .subtract(new BigDecimal(detectRlVO.getSi() + "")))
                            .divide(new BigDecimal(si.getEAbsorb()+"")
                                    .multiply(new BigDecimal(si.getEEffective()+"")),2,BigDecimal.ROUND_HALF_UP)
                            ;
                    log.info("补加硅铁:" + sibjl.toString() +"原铁水标准min：" + detectRlVO.getSiMin()+"实测值：" + detectRlVO.getSi() + "加料总重：" + jlzl);
                }
            }else {
                color = "0002";
            }

        }else {
            if( detectRlVO.getSi() > detectRlVO.getSiMaxZh()){
                color = "0001";
            }else if(detectRlVO.getSi() < detectRlVO.getSiMinZh()){
                color = "0003";
                if(si != null){
                    sibjl = new BigDecimal(jlzl + "").multiply(
                            new BigDecimal(detectRlVO.getSiMinZh() + "")
                                    .subtract(new BigDecimal(detectRlVO.getSi() + "")))
                            .divide(new BigDecimal(si.getEAbsorb()+"").multiply(new BigDecimal(si.getEEffective()+"")),2,BigDecimal.ROUND_HALF_UP)
                            ;
                    log.info("补加硅铁:" + sibjl.toString() +"出铁水标准min：" + detectRlVO.getSiMinZh()+"实测值：" + detectRlVO.getSi() + "加料总重：" + jlzl);
                }
            }else {
                color = "0002";
            }
        }
//        10-16  17 -23  24 - 30
        log.info("Si:" + detectRlVO.getSi().toString());
        Thread.sleep(200);//休息200毫秒
        detectRlVO.setSi(new BigDecimal(detectRlVO.getSi()+"").setScale(3,BigDecimal.ROUND_HALF_UP).doubleValue());
        if(furnaceNum.equals("01") || furnaceNum.equals("1")){
            TcpClientPusDuan.push(zhu1ip,zhu1port,zk11 + color + chineseToHex(detectRlVO.getSi().toString()),false);
        }else if(furnaceNum.equals("02") || furnaceNum.equals("2")){
            TcpClientPusDuan.push(zhu1ip,zhu1port,zk18 + color + chineseToHex(detectRlVO.getSi().toString()),false);
        }else if(furnaceNum.equals("05") || furnaceNum.equals("5")){
            TcpClientPusDuan.push(zhu2ip,zhu2port,zk11 + color + chineseToHex(detectRlVO.getSi().toString()),false);
        }else if(furnaceNum.equals("06") || furnaceNum.equals("6")){
            TcpClientPusDuan.push(zhu2ip,zhu2port,zk18 + color + chineseToHex(detectRlVO.getSi().toString()),false);
        }else if(furnaceNum.equals("07") || furnaceNum.equals("7")){
            TcpClientPusDuan.push(zhu2ip,zhu2port,zk25 + color + chineseToHex(detectRlVO.getSi().toString()),false);
        }
        /**
         * mn
         */
        ElementVO mn = elementVOHashMap.get("Mn");
        if(zjlx.equals("原铁水")){
            if( detectRlVO.getMn() > detectRlVO.getMnMax()){
                color = "0001";
            }else if(detectRlVO.getMn() < detectRlVO.getMnMin()){
                color = "0003";
                if(mn != null){
                    mnbjl = new BigDecimal(jlzl + "").multiply(
                            new BigDecimal(detectRlVO.getMnMin() + "")
                                    .subtract(new BigDecimal(detectRlVO.getMn() + "")))
                            .divide(new BigDecimal(mn.getEAbsorb()+"").multiply(new BigDecimal(mn.getEEffective()+"")),2,BigDecimal.ROUND_HALF_UP)
                            ;
                    log.info("补加锰:" + mnbjl.toString() +"原铁水标准min：" + detectRlVO.getMnMin()+"实测值：" + detectRlVO.getMn() + "加料总重：" + jlzl);
                }
            }else {
                color = "0002";
            }

        }else {
            if( detectRlVO.getMn() > detectRlVO.getMnMaxZh()){
                color = "0001";
            }else if(detectRlVO.getMn() < detectRlVO.getMnMinZh()){
                color = "0003";
                if(mn != null){
                    mnbjl = new BigDecimal(jlzl + "").multiply(
                            new BigDecimal(detectRlVO.getMnMinZh() + "")
                                    .subtract(new BigDecimal(detectRlVO.getMn() + "")))
                            .divide(new BigDecimal(mn.getEAbsorb()+"").multiply(new BigDecimal(mn.getEEffective()+"")),2,BigDecimal.ROUND_HALF_UP)
                            ;
                    log.info("补加锰:" + mnbjl.toString() +"出铁水标准min：" + detectRlVO.getMnMinZh()+"实测值：" + detectRlVO.getMn() + "加料总重：" + jlzl);
                }
            }else {
                color = "0002";
            }
        }
        //        10-16  17 -23  24 - 30
        log.info("Mn:" + detectRlVO.getMn().toString());
        Thread.sleep(200);//休息200毫秒
        detectRlVO.setMn(new BigDecimal(detectRlVO.getMn()+"").setScale(3,BigDecimal.ROUND_HALF_UP).doubleValue());
        if(furnaceNum.equals("01") || furnaceNum.equals("1")){
            TcpClientPusDuan.push(zhu1ip,zhu1port,zk12 + color + chineseToHex(detectRlVO.getMn().toString()),false);
        }else if(furnaceNum.equals("02") || furnaceNum.equals("2")){
            TcpClientPusDuan.push(zhu1ip,zhu1port,zk19 + color + chineseToHex(detectRlVO.getMn().toString()),false);
        }else if(furnaceNum.equals("05") || furnaceNum.equals("5")){
            TcpClientPusDuan.push(zhu2ip,zhu2port,zk12 + color + chineseToHex(detectRlVO.getMn().toString()),false);
        }else if(furnaceNum.equals("06") || furnaceNum.equals("6")){
            TcpClientPusDuan.push(zhu2ip,zhu2port,zk19 + color + chineseToHex(detectRlVO.getMn().toString()),false);
        }else if(furnaceNum.equals("07") || furnaceNum.equals("7")){
            TcpClientPusDuan.push(zhu2ip,zhu2port,zk26 + color + chineseToHex(detectRlVO.getMn().toString()),false);
        }

        /**
         * s
         */
        ElementVO s = elementVOHashMap.get("S");
        if(zjlx.equals("原铁水")){
            if( detectRlVO.getS() > detectRlVO.getSMax()){
                color = "0001";
            }else if(detectRlVO.getS() < detectRlVO.getSMin()){
                color = "0003";
                if(s != null){
                    sbjl = new BigDecimal(jlzl + "").multiply(
                            new BigDecimal(detectRlVO.getSMin() + "")
                                    .subtract(new BigDecimal(detectRlVO.getS() + "")))
                            .divide(new BigDecimal(s.getEAbsorb()+"").multiply(new BigDecimal(s.getEEffective()+"")),2,BigDecimal.ROUND_HALF_UP)
                            ;
                    log.info("补加硫:" + sbjl.toString() +"原铁水标准min：" + detectRlVO.getSMin()+"实测值：" + detectRlVO.getS() + "加料总重：" + jlzl);
                }
            }else {
                color = "0002";
            }
        }else {
            if( detectRlVO.getS() > detectRlVO.getSMaxZh()){
                color = "0001";
            }else if(detectRlVO.getS() < detectRlVO.getSMinZh()){
                color = "0003";
                if(s != null){
                    sbjl = new BigDecimal(jlzl + "").multiply(
                            new BigDecimal(detectRlVO.getSMinZh() + "")
                                    .subtract(new BigDecimal(detectRlVO.getS() + "")))
                            .divide(new BigDecimal(s.getEAbsorb()+"").multiply(new BigDecimal(s.getEEffective()+"")),2,BigDecimal.ROUND_HALF_UP)
                            ;
                    log.info("补加硫:" + sbjl.toString() +"出铁水标准min：" + detectRlVO.getSMinZh()+"实测值：" + detectRlVO.getS() + "加料总重：" + jlzl);
                }
            }else {
                color = "0002";
            }
        }
        //        10-16  17 -23  24 - 30
        log.info("S:" + detectRlVO.getS().toString());
        Thread.sleep(200);//休息200毫秒
        detectRlVO.setS(new BigDecimal(detectRlVO.getS()+"").setScale(3,BigDecimal.ROUND_HALF_UP).doubleValue());
        if(furnaceNum.equals("01") || furnaceNum.equals("1")){
            TcpClientPusDuan.push(zhu1ip,zhu1port,zk13 + color + chineseToHex(detectRlVO.getS().toString()),false);
        }else if(furnaceNum.equals("02") || furnaceNum.equals("2")){
            TcpClientPusDuan.push(zhu1ip,zhu1port,zk20 + color + chineseToHex(detectRlVO.getS().toString()),false);
        }else if(furnaceNum.equals("05") || furnaceNum.equals("5")){
            TcpClientPusDuan.push(zhu2ip,zhu2port,zk13 + color + chineseToHex(detectRlVO.getS().toString()),false);
        }else if(furnaceNum.equals("06") || furnaceNum.equals("6")){
            TcpClientPusDuan.push(zhu2ip,zhu2port,zk20 + color + chineseToHex(detectRlVO.getS().toString()),false);
        }else if(furnaceNum.equals("07") || furnaceNum.equals("7")){
            TcpClientPusDuan.push(zhu2ip,zhu2port,zk27 + color + chineseToHex(detectRlVO.getS().toString()),false);
        }
        /**
         * P
         */
        ElementVO p = elementVOHashMap.get("P");
        if(zjlx.equals("原铁水")){
            if( detectRlVO.getP() > detectRlVO.getPMax()){
                color = "0001";
            }else if(detectRlVO.getP() < detectRlVO.getPMin()){
                color = "0003";
                if(p != null){
                    pbjl = new BigDecimal(jlzl + "").multiply(
                            new BigDecimal(detectRlVO.getPMin() + "")
                                    .subtract(new BigDecimal(detectRlVO.getP() + "")))
                            .divide(new BigDecimal(p.getEAbsorb()+"").multiply(new BigDecimal(p.getEEffective()+"")),2,BigDecimal.ROUND_HALF_UP)
                            ;
                    log.info("补加磷:" + pbjl.toString() +"原铁水标准min：" + detectRlVO.getPMin()+"实测值：" + detectRlVO.getP() + "加料总重：" + jlzl);
                }
            }else {
                color = "0002";
            }

        }else {
            if( detectRlVO.getP() > detectRlVO.getPMaxZh()){
                color = "0001";
            }else if(detectRlVO.getP() < detectRlVO.getPMinZh()){
                color = "0003";
                if(p != null){
                    pbjl = new BigDecimal(jlzl + "").multiply(
                            new BigDecimal(detectRlVO.getPMinZh() + "")
                                    .subtract(new BigDecimal(detectRlVO.getP() + "")))
                            .divide(new BigDecimal(p.getEAbsorb()+"").multiply(new BigDecimal(p.getEEffective()+"")),2,BigDecimal.ROUND_HALF_UP)
                            ;
                    log.info("补加磷:" + pbjl.toString() +"出铁水标准min：" + detectRlVO.getPMinZh()+"实测值：" + detectRlVO.getP() + "加料总重：" + jlzl);
                }
            }else {
                color = "0002";
            }
        }

        //        10-16  17 -23  24 - 30
        log.info("P:" + detectRlVO.getP().toString());
        detectRlVO.setP(new BigDecimal(detectRlVO.getP()+"").setScale(3,BigDecimal.ROUND_HALF_UP).doubleValue());
        if(furnaceNum.equals("01") || furnaceNum.equals("1")){
            TcpClientPusDuan.push(zhu1ip,zhu1port,zk14 + color + chineseToHex(detectRlVO.getP().toString()),false);
        }else if(furnaceNum.equals("02") || furnaceNum.equals("2")){
            TcpClientPusDuan.push(zhu1ip,zhu1port,zk21 + color + chineseToHex(detectRlVO.getP().toString()),false);
        }else if(furnaceNum.equals("05") || furnaceNum.equals("5")){
            TcpClientPusDuan.push(zhu2ip,zhu2port,zk14 + color + chineseToHex(detectRlVO.getP().toString()),false);
        }else if(furnaceNum.equals("06") || furnaceNum.equals("6")){
            TcpClientPusDuan.push(zhu2ip,zhu2port,zk21 + color + chineseToHex(detectRlVO.getP().toString()),false);
        }else if(furnaceNum.equals("07") || furnaceNum.equals("7")){
            TcpClientPusDuan.push(zhu2ip,zhu2port,zk28 + color + chineseToHex(detectRlVO.getP().toString()),false);
        }
        /**
         * Cr
         */
        ElementVO cr = elementVOHashMap.get("Cr");
        if(zjlx.equals("原铁水")){
            if( detectRlVO.getCr() > detectRlVO.getCrMax()){
                color = "0001";
            }else if(detectRlVO.getCr() < detectRlVO.getCrMin()){
                color = "0003";
                if(cr != null){
                    crbjl = new BigDecimal(jlzl + "").multiply(
                            new BigDecimal(detectRlVO.getCrMin() + "")
                                    .subtract(new BigDecimal(detectRlVO.getCr() + "")))
                            .divide(new BigDecimal(cr.getEAbsorb()+"").multiply(new BigDecimal(cr.getEEffective()+"")),2,BigDecimal.ROUND_HALF_UP)
                            ;
                    log.info("补加铬:" + crbjl.toString() +"原铁水标准min：" + detectRlVO.getCrMin()+"实测值：" + detectRlVO.getCr() + "加料总重：" + jlzl);
                }
            }else {
                color = "0002";
            }

        }else {
            if( detectRlVO.getCr() > detectRlVO.getCrMaxZh()){
                color = "0001";
            }else if(detectRlVO.getCr() < detectRlVO.getCrMinZh()){
                color = "0003";
                if(cr != null){
                    crbjl = new BigDecimal(jlzl + "").multiply(
                            new BigDecimal(detectRlVO.getCrMinZh() + "")
                                    .subtract(new BigDecimal(detectRlVO.getCr() + "")))
                            .divide(new BigDecimal(cr.getEAbsorb()+"").multiply(new BigDecimal(cr.getEEffective()+"")),2,BigDecimal.ROUND_HALF_UP)
                            ;
                    log.info("补加铬:" + crbjl.toString() +"出铁水标准min：" + detectRlVO.getCrMinZh()+"实测值：" + detectRlVO.getCr() + "加料总重：" + jlzl);
                }
            }else {
                color = "0002";
            }
        }
        //        10-16  17 -23  24 - 30
        log.info("Cr:" + detectRlVO.getCr().toString());
        Thread.sleep(200);//休息200毫秒
        detectRlVO.setCr(new BigDecimal(detectRlVO.getCr()+"").setScale(3,BigDecimal.ROUND_HALF_UP).doubleValue());
        if(furnaceNum.equals("01") || furnaceNum.equals("1")){
            TcpClientPusDuan.push(zhu1ip,zhu1port,zk15 + color + chineseToHex(detectRlVO.getCr().toString()),false);
        }else if(furnaceNum.equals("02") || furnaceNum.equals("2")){
            TcpClientPusDuan.push(zhu1ip,zhu1port,zk22 + color + chineseToHex(detectRlVO.getCr().toString()),false);
        }else if(furnaceNum.equals("05") || furnaceNum.equals("5")){
            TcpClientPusDuan.push(zhu2ip,zhu2port,zk15 + color + chineseToHex(detectRlVO.getCr().toString()),false);
        }else if(furnaceNum.equals("06") || furnaceNum.equals("6")){
            TcpClientPusDuan.push(zhu2ip,zhu2port,zk22 + color + chineseToHex(detectRlVO.getCr().toString()),false);
        }else if(furnaceNum.equals("07") || furnaceNum.equals("7")){
            TcpClientPusDuan.push(zhu2ip,zhu2port,zk29 + color + chineseToHex(detectRlVO.getCr().toString()),false);
        }
        /**
         * cu
         */
        ElementVO cu = elementVOHashMap.get("Cu");
        if(zjlx.equals("原铁水")){
            if( detectRlVO.getCu() > detectRlVO.getCuMax()){
                color = "0001";
            }else if(detectRlVO.getCu() < detectRlVO.getCuMin()){
                color = "0003";
                if(cu != null){
                    cubjl = new BigDecimal(jlzl + "").multiply(
                            new BigDecimal(detectRlVO.getCuMin() + "")
                                    .subtract(new BigDecimal(detectRlVO.getCu() + "")))
                            .divide(new BigDecimal(cu.getEAbsorb()+"").multiply(new BigDecimal(cu.getEEffective()+"")),2,BigDecimal.ROUND_HALF_UP)
                            ;
                    log.info("补加铜:" + cubjl.toString() +"原铁水标准min：" + detectRlVO.getCuMin()+"实测值：" + detectRlVO.getCu() + "加料总重：" + jlzl);
                }
            }else {
                color = "0002";
            }

        }else {
            if( detectRlVO.getCu() > detectRlVO.getCuMaxZh()){
                color = "0001";
            }else if(detectRlVO.getCu() < detectRlVO.getCuMinZh()){
                color = "0003";
                if(cu != null){
                    cubjl = new BigDecimal(jlzl + "").multiply(
                            new BigDecimal(detectRlVO.getCuMinZh() + "")
                                    .subtract(new BigDecimal(detectRlVO.getCu() + "")))
                            .divide(new BigDecimal(cu.getEAbsorb()+"").multiply(new BigDecimal(cu.getEEffective()+"")),2,BigDecimal.ROUND_HALF_UP)
                            ;
                    log.info("补加铜:" + cubjl +"出铁水标准min：" + detectRlVO.getCuMinZh()+"实测值：" + detectRlVO.getCu() + "加料总重：" + jlzl);
                }

            }else {
                color = "0002";
            }
        }
        //        10-16  17 -23  24 - 30
        log.info("Cu:" + detectRlVO.getCu().toString());
        Thread.sleep(200);//休息200毫秒
        detectRlVO.setCu(new BigDecimal(detectRlVO.getCu()+"").setScale(3,BigDecimal.ROUND_HALF_UP).doubleValue());
        if(furnaceNum.equals("01") || furnaceNum.equals("1")){
            TcpClientPusDuan.push(zhu1ip,zhu1port,zk16 + color + chineseToHex(detectRlVO.getCu().toString()),false);
        }else if(furnaceNum.equals("02") || furnaceNum.equals("2")){
            TcpClientPusDuan.push(zhu1ip,zhu1port,zk23 + color + chineseToHex(detectRlVO.getCu().toString()),false);
        }else if(furnaceNum.equals("05") || furnaceNum.equals("5")){
            TcpClientPusDuan.push(zhu2ip,zhu2port,zk16 + color + chineseToHex(detectRlVO.getCu().toString()),false);
        }else if(furnaceNum.equals("06") || furnaceNum.equals("6")){
            TcpClientPusDuan.push(zhu2ip,zhu2port,zk23 + color + chineseToHex(detectRlVO.getCu().toString()),false);
        }else if(furnaceNum.equals("07") || furnaceNum.equals("7")){
            TcpClientPusDuan.push(zhu2ip,zhu2port,zk30 + color + chineseToHex(detectRlVO.getCu().toString()),false);
        }

        /**
         * 补加量
         */
        String zu1bujl = "00 01 00 00 00 1B 01 10 00 FA 00 07 0E";
        String zu2bujl = "00 01 00 00 00 1B 01 10 01 04 00 07 0E";
        String zu3bujl = "00 01 00 00 00 1B 01 10 01 0E 00 07 0E";
        //        10-16  17 -23  24 - 30
        log.info("加料重量" + jlzl +" 补加量:" + " C：" + cbjl.toString() + " Si:" + sibjl.toString()
                + " Mn:" + mnbjl.toString() +" S:" + sbjl.toString() + " P:" + pbjl.toString() + " Cr:" +crbjl.toString()
                +" Cu:" + cubjl.toString() );
        if(furnaceNum.equals("01") || furnaceNum.equals("1")){
            TcpClientPusDuan.push(zhu1ip,zhu1port,zu1bujl
                    + NumberUtils.int10to16_4(cbjl.intValue())
                    + NumberUtils.int10to16_4(sibjl.intValue())
                    + NumberUtils.int10to16_4(mnbjl.intValue())
                    + NumberUtils.int10to16_4(sbjl.intValue())
                    + NumberUtils.int10to16_4(pbjl.intValue())
                    + NumberUtils.int10to16_4(crbjl.intValue())
                    + NumberUtils.int10to16_4(cubjl.intValue()),
                    zu1bujl
            );
        }else if(furnaceNum.equals("02") || furnaceNum.equals("2")){
            TcpClientPusDuan.push(zhu1ip,zhu1port,zu2bujl
                    + NumberUtils.int10to16_4(cbjl.intValue())
                    + NumberUtils.int10to16_4(sibjl.intValue())
                    + NumberUtils.int10to16_4(mnbjl.intValue())
                    + NumberUtils.int10to16_4(sbjl.intValue())
                    + NumberUtils.int10to16_4(pbjl.intValue())
                    + NumberUtils.int10to16_4(crbjl.intValue())
                    + NumberUtils.int10to16_4(cubjl.intValue()),
                    zu2bujl);
        }else if(furnaceNum.equals("05") || furnaceNum.equals("5")){
            TcpClientPusDuan.push(zhu2ip,zhu2port,zu1bujl
                    + NumberUtils.int10to16_4(cbjl.intValue())
                    + NumberUtils.int10to16_4(sibjl.intValue())
                    + NumberUtils.int10to16_4(mnbjl.intValue())
                    + NumberUtils.int10to16_4(sbjl.intValue())
                    + NumberUtils.int10to16_4(pbjl.intValue())
                    + NumberUtils.int10to16_4(crbjl.intValue())
                    + NumberUtils.int10to16_4(cubjl.intValue())
            ,zu1bujl);
        }else if(furnaceNum.equals("06") || furnaceNum.equals("6")){
            TcpClientPusDuan.push(zhu2ip,zhu2port,zu2bujl
                    + NumberUtils.int10to16_4(cbjl.intValue())
                    + NumberUtils.int10to16_4(sibjl.intValue())
                    + NumberUtils.int10to16_4(mnbjl.intValue())
                    + NumberUtils.int10to16_4(sbjl.intValue())
                    + NumberUtils.int10to16_4(pbjl.intValue())
                    + NumberUtils.int10to16_4(crbjl.intValue())
                    + NumberUtils.int10to16_4(cubjl.intValue()),  zu2bujl);
        }else if(furnaceNum.equals("07") || furnaceNum.equals("7")){
            TcpClientPusDuan.push(zhu2ip,zhu2port,zu3bujl
                    + NumberUtils.int10to16_4(cbjl.intValue())
                    + NumberUtils.int10to16_4(sibjl.intValue())
                    + NumberUtils.int10to16_4(mnbjl.intValue())
                    + NumberUtils.int10to16_4(sbjl.intValue())
                    + NumberUtils.int10to16_4(pbjl.intValue())
                    + NumberUtils.int10to16_4(crbjl.intValue())
                    + NumberUtils.int10to16_4(cubjl.intValue()),zu3bujl);
        }

    }
    public void receive(String tableName,String id){
        try{
            if(tableName.equals("tb_upload_truck_feeding")){//配方信息【主料和辅料重量】和实时加料
                Feeding feeding = detectionRlMapper.getFeedingById(Long.valueOf(id));
                if(feeding.getJlType() == 2){
                    log.info("加的辅料,不推送!");
                    return;
                }
                TbUploadTruckFeedingVO tbUploadTruckFeedingVO = detectionRlMapper.getFeedingPfById(Long.valueOf(id));
                if(tbUploadTruckFeedingVO == null){
                    log.error("质检数据关联为空，sql:" +
                            "select * from v_tb_upload_truck_feeding  fe left join v_tb_formula_detail_master pf on fe.lable1 = pf.formula where  fe.id = " + id);
                    return;
                }
                tbUploadTruckFeedingVO.setId(id);
                pushFeeding(tbUploadTruckFeedingVO);
            }else if(tableName.equals("tb_detection_rl")){
                DetectRlVO detectRl = detectionRlMapper.getDetectRlById(Long.valueOf(id));
                if(detectRl ==  null){
                    log.error("质检数据关联查询为空,sql："
                            + "from  tb_detection_rl rl " +
                            "        LEFT JOIN tb_smelter_melter_standard sd  on rl.lable1 = sd.cz " +
                            "        LEFT JOIN tb_smelter_melter_standard_zts sdz on rl.lable1 = sdz.cz " +
                            "        where rl.id =" + id);
                    return;
                }else if(detectRl.getSdId() == null){
                    log.error("原铁水成分未关联上,sql：" + "from  tb_detection_rl rl\n" +
                            "        LEFT JOIN tb_smelter_melter_standard sd  on rl.lable1 = sd.cz\n" +
                            "        LEFT JOIN tb_smelter_melter_standard_zts sdz on rl.lable1 = sdz.cz\n" +
                            "        where rl.id =" + id);
                    log.error("原铁水成分未关联上！");
                }else if(detectRl.getSdzId() == null){
                    log.error("出铁水成分未关联上,sql：" + "from  tb_detection_rl rl\n" +
                            "        LEFT JOIN tb_smelter_melter_standard sd  on rl.lable1 = sd.cz\n" +
                            "        LEFT JOIN tb_smelter_melter_standard_zts sdz on rl.lable1 = sdz.cz\n" +
                            "        where rl.id =" + id);
                    log.error("出铁水成分未关联上！");
                }
                pushDetectRl(detectRl);
            }else if(tableName.equals("tb_melting_measurement")){
                TbMeltingMeasurementVO measurementVO = detectionRlMapper.getMeasurementRlById(Long.valueOf(id));
                pushMeltingMeasurement(measurementVO);
            }else{
                log.info("监听表无效：" + tableName );
            }
        }catch (Exception exception){
            System.out.println("异常：" + exception);
            log.error(exception.getMessage());
        }
    }
    public static void main(String[] args) {
        System.out.println(chineseToHex(" "));
        System.out.println(chineseToHex("_"));
        System.out.println(chineseToHex("GG20HC"));
        System.out.println(chineseToHex("G3000"));
        System.out.println(chineseToHex("241225_103A"));
        System.out.println(chineseToHex("241225_103A").length());
//        TcpClientPusDuan.push("10.30.13.249","5003","00 01 00 00 00 1B 00 10 00 1F 00 0B 16 3234313232355f31303341".replaceAll(" ",""),false);//材质
//        System.out.println("241225_103A".substring(2));


    }
}
